#include <wx/wx.h>
#include <wx/utils.h>
#include <wx/file.h>
#include <wx/textfile.h>
#include <wx/dir.h>
#include <iostream>
#include <string>
#include <vector>
#include <map>

class AICommunicationSystem {
private:
    std::vector<std::string> conversation;
    
public:
    void StartInteractive() {
        std::cout << "🚀 AI Communication System" << std::endl;
        std::cout << "==========================" << std::endl;
        std::cout << "Human ↔ AI ↔ Computer" << std::endl;
        std::cout << std::endl;
        
        char buffer[512];
        while (true) {
            std::cout << "👤 Human> ";
            std::flush(std::cout);
            
            if (!std::cin.getline(buffer, sizeof(buffer))) {
                break;
            }
            
            std::string input(buffer);
            if (input == "exit" || input == "quit") {
                std::cout << "👋 Goodbye!" << std::endl;
                break;
            }
            else if (input == "help") {
                ShowHelp();
            }
            else if (input == "status") {
                ShowStatus();
            }
            else if (input == "files") {
                ListFiles();
            }
            else if (input == "wxinfo") {
                ShowWxInfo();
            }
            else if (!input.empty()) {
                ProcessInput(input);
            }
        }
    }
    
    void ProcessInput(const std::string& input) {
        conversation.push_back("Human: " + input);
        std::cout << "🤖 AI: Processing command..." << std::endl;
        
        // AI 分析
        if (input.find("read") != std::string::npos) {
            std::cout << "📖 Reading operation" << std::endl;
        }
        else if (input.find("write") != std::string::npos) {
            std::cout << "📝 Writing operation" << std::endl;
        }
        else if (input.find("compile") != std::string::npos) {
            std::cout << "🔨 Compile operation" << std::endl;
        }
        
        // 执行计算机命令
        ExecuteComputerCommand(input);
    }
    
    void ExecuteComputerCommand(const std::string& command) {
        std::cout << "🖥️ Computer: Executing..." << std::endl;
        
        // 使用 wxWidgets 功能
        wxString cwd = wxGetCwd();
        std::cout << "   Current dir: " << (const char*)cwd.mb_str() << std::endl;
        
        conversation.push_back("Computer: Executed " + command);
    }
    
    void ShowHelp() {
        std::cout << "📖 Available Commands:" << std::endl;
        std::cout << "  read <file>    - Read file" << std::endl;
        std::cout << "  write <file>   - Write file" << std::endl;
        std::cout << "  compile <proj> - Compile project" << std::endl;
        std::cout << "  status         - System status" << std::endl;
        std::cout << "  files          - List files" << std::endl;
        std::cout << "  wxinfo         - wxWidgets info" << std::endl;
        std::cout << "  help           - This help" << std::endl;
        std::cout << "  exit           - Exit" << std::endl;
    }
    
    void ShowStatus() {
        std::cout << "📊 System Status:" << std::endl;
        std::cout << "  wxWidgets: " << wxVERSION_STRING << std::endl;
        std::cout << "  Directory: " << (const char*)wxGetCwd().mb_str() << std::endl;
        std::cout << "  Messages: " << conversation.size() << std::endl;
    }
    
    void ListFiles() {
        std::cout << "📁 Files:" << std::endl;
        wxString cwd = wxGetCwd();
        wxDir dir(cwd);
        
        if (dir.IsOpened()) {
            wxString filename;
            bool cont = dir.GetFirst(&filename, "*.cpp", wxDIR_FILES);
            while (cont) {
                std::cout << "  " << (const char*)filename.mb_str() << std::endl;
                cont = dir.GetNext(&filename);
            }
        }
    }
    
    void ShowWxInfo() {
        std::cout << "🔧 wxWidgets Info:" << std::endl;
        std::cout << "  Version: " << wxVERSION_STRING << std::endl;
    }
};

int main(int argc, char* argv[]) {
    wxInitialize();
    
    AICommunicationSystem ai;
    
    if (argc > 1) {
        std::string arg = argv[1];
        if (arg == "--interactive") {
            ai.StartInteractive();
        }
        else if (arg == "--status") {
            ai.ShowStatus();
        }
        else if (arg == "--help") {
            ai.ShowHelp();
        }
        else {
            std::cout << "Use --help for usage" << std::endl;
        }
    } else {
        ai.StartInteractive();
    }
    
    wxUninitialize();
    return 0;
}
